import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { lampcontrolState } from './typings'
import {
  lampcontrolListGet,
  lampcontrolRemovePost,
  lampcontrolExportGet,
  lampcontrolImportExcelGet,
  lampcontrolModelGet,
  lampcontrolViewGet,
  lampcontrolAddPost,
  lampcontrolUpdatePost,
  lampcontrolExportTemplateGet
} from '~/api/asset/lampcontrol'
import { DownloadAttachment } from '~/utils/util'

const lampcontrol: Module<lampcontrolState, lampcontrolState> = {
  namespaced: true,
  state: {
    lampcontrolModelOpt: []
  },
  getters: {
    lampcontrolModelOpt: (state) => state.lampcontrolModelOpt
  },
  mutations: {
    SET_LAMPCONTROL_MODEL: (state, payload) => {
      state.lampcontrolModelOpt = payload
    }
  },
  actions: {
    // 单灯控制器-型号
    async LampcontrolModelGet<S, T>({ commit }: ActionContext<S, T>) {
      return await lampcontrolModelGet({}).then((res: AxiosResponse) => {
        commit('SET_LAMPCONTROL_MODEL', res?.data?.data || [])
        return res?.data
      })
    },
    // 单灯控制器-列表
    async LampcontrolListGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await lampcontrolListGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 单灯控制器-删除
    async LampcontrolRemovePost<S, T>(_: ActionContext<S, T>, params: T) {
      return await lampcontrolRemovePost<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 单灯控制器-导出
    async LampcontrolExportGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await lampcontrolExportGet<T>(params).then((res: AxiosResponse) => {
        const contentDisposition = res?.headers?.['content-disposition']
        if (contentDisposition && res.data) {
          DownloadAttachment(decodeURI(contentDisposition.substring(20)), res.data)
        }
        return res.data
      })
    },
    // 单灯控制器-模版下载
    async LampcontrolExportTemplateGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await lampcontrolExportTemplateGet<T>(params).then((res: AxiosResponse) => {
        const contentDisposition = res?.headers?.['content-disposition']
        if (contentDisposition && res.data) {
          DownloadAttachment(decodeURI(contentDisposition.substring(20)), res.data)
        }
        return res.data
      })
    },
    // 单灯控制器-导入
    async LampcontrolImportExcelGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await lampcontrolImportExcelGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 单灯控制器-详情
    async LampcontrolViewGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await lampcontrolViewGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 单灯控制器-新增
    async LampcontrolAddPost<S, T>(_: ActionContext<S, T>, params: T) {
      return await lampcontrolAddPost<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 单灯控制器-修改
    async LampcontrolUpdatePost<S, T>(_: ActionContext<S, T>, params: T) {
      return await lampcontrolUpdatePost<T>(params).then((res: AxiosResponse) => res?.data)
    }
  }
}

export default lampcontrol
